package com.excesys.exsecs.energy.controller;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.security.annotation.Inner;
import com.excesys.exsecs.energy.service.*;
import com.excesys.exsecs.energy.task.PowerTask;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author zhangtailong
 * @title: EcAnalyzeController
 * @projectName pigx
 * @description: 中台 -- 能耗费用分析
 * @date 2024/10/28 10:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/eccost")
@Tag(name = "能耗费用")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EcCostController {

    //部门维度表和设备映射表
    private final DevDeptMapService devDeptMapService;
    //部门维度表
    private final DimDeptService dimDeptService;
    //区域维度表和设备映射表
    private final DevAreaMapService devAreaMapService;
    //区域维度表
    private final DimAreaService dimAreaService;
    //重点设备维度表和设备映射表
    private final DevKeqMapService devKeqMapService;
    //重点设备表
    private final DimKeqService dimKeqService;
    //分项维度表和设备映射表
    private final DevSubitemMapService devSubitemMapService;
    //分项维度表
    private final DimSubitemService dimSubitemService;
    //支路维度表和设备映射表
    private final DevBranchMapService devBranchMapService;
    //支路维度表
    private final DimBranchService dimBranchService;

    private final PowerTask powerTask;

    /*
     * 能耗费用
     * @param dimType 查询 1部门 2区域 3分项 4支路 5设备
     * @param id 查询id
     * @param startDate 开始时间 yyyy、yyyy-MM、yyyy-MM-dd
     * @param endDate 开始时间 yyyy、yyyy-MM、yyyy-MM-dd
     * */
    @Operation(summary = "能耗费用", description = "能耗费用")
    @GetMapping("/powerCostData")
    public R powerCostData(String dimType, String id, String startDate, String endDate) throws ParseException {
        JSONArray result = new JSONArray();
        if (dimType.equals("1")) {
            result = devDeptMapService.powerCostData(id, startDate, endDate);
        } else if (dimType.equals("2")) {
            result = devAreaMapService.powerCostData(id, startDate, endDate);
        } else if (dimType.equals("3")) {
            result = devSubitemMapService.powerCostData(id, startDate, endDate);
        } else if (dimType.equals("4")) {
            result = devBranchMapService.powerCostData(id, startDate, endDate);
        } else if (dimType.equals("5")) {
            result = devKeqMapService.powerCostData(id, startDate, endDate);
        }
        return R.ok(result, "能耗费用");
    }

    /*
     * 能耗费用饼状图
     * @param dimType 查询 1部门 2区域 3分项 4支路 5设备
     * @param id 查询id
     * @param startDate 开始时间 yyyy、yyyy-MM、yyyy-MM-dd
     * @param endDate 开始时间 yyyy、yyyy-MM、yyyy-MM-dd
     * */
    @Operation(summary = "能耗费用饼状图", description = "能耗费用")
    @GetMapping("/powerCostBingtu")
    public R powerCostBingtu(String dimType, String id, String startDate, String endDate) throws ParseException {
        JSONObject result = new JSONObject();
        if (dimType.equals("1")) {
            result = devDeptMapService.powerCostBingtu(id, startDate, endDate);
        } else if (dimType.equals("2")) {
            result = devAreaMapService.powerCostBingtu(id, startDate, endDate);
        } else if (dimType.equals("3")) {
            result = devSubitemMapService.powerCostBingtu(id, startDate, endDate);
        } else if (dimType.equals("4")) {
            result = devBranchMapService.powerCostBingtu(id, startDate, endDate);
        } else if (dimType.equals("5")) {
            result = devKeqMapService.powerCostBingtu(id, startDate, endDate);
        }
        return R.ok(result, "能耗费用");
    }


    @GetMapping("/insertCostFlowHandler")
    @Inner(value = false)
    public R insertCostFlowHandler() throws ParseException {
        powerTask.insertCostFlowHandler();
        return R.ok(null, "能耗费用");
    }


}
